import { fileURLToPath } from 'url'
import fs from 'fs'
import path from 'path'
export function getController(dirPath) {
    let targetPath = new URL(dirPath, path.dirname(new URL(import.meta.url).href))
    let pathArr = []
    if (targetPath) {
        fs.readdirSync(targetPath).filter(item => {
            if (item.endsWith('.js')) {
                let file = `${targetPath}/${item}`
                pathArr.push(file)
            }
        })
        return pathArr;
    }
}

export function getRouter(router, controller) {
    controller.forEach(async item => {
        let { user } = await import(item)
        for (let key in user) {
            let [method, path] = key.split(' ')
            let fn = user[key]
            if (method === 'get') {
                router.get(path, fn)
            }
            if (method === 'post') {
                router.post(path, fn)
            }
            if (method === 'put') {                
                router.put(path, fn)
            }
            if (method === 'delete') {
                router.delete(path, fn)
            }
        }
    })
}